﻿using System;
using System.Windows.Forms;

namespace ThirdHand
{
    public partial class ConfigureCommandsForm : Form
    {
        public ConfigureCommandsForm()
        {
            InitializeComponent();
        }

        private void SaveAndCloseButton_Click(object sender, EventArgs e)
        {
            thirdHandCommandBindingListBindingSource.EndEdit();
            ThirdHandCommandBindingList thcbl = thirdHandCommandBindingListBindingSource.DataSource as ThirdHandCommandBindingList;
            if (thcbl != null) thcbl.SaveToStorage();
            Close();
        }

        private void ConfigureCommandsForm_Shown(object sender, EventArgs e)
        {
            ThirdHandCommandBindingList thcbl = ThirdHandCommandBindingList.LoadFromStorage();
            thirdHandCommandBindingListBindingSource.DataSource = thcbl;
        }

        private void CancelWithoutSavingButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ExportButton_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog(this)== DialogResult.OK)
            {
                thirdHandCommandBindingListBindingSource.EndEdit();
                ThirdHandCommandBindingList thcbl = thirdHandCommandBindingListBindingSource.DataSource as ThirdHandCommandBindingList;

                if (thcbl != null) thcbl.SaveToFile(saveFileDialog1.FileName);
            }
        }

        private void RevertButton_Click(object sender, EventArgs e)
        {
            MessageBoxOptions mbo=0;
            if (RightToLeft == RightToLeft.Yes)
            {
                mbo = MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading;
            }
            if (MessageBox.Show(Properties.Resources.RevertWarningMessage, Properties.Resources.RevertWarningTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2,mbo) == DialogResult.Yes)
            {
                thirdHandCommandBindingListBindingSource.Clear();
                thirdHandCommandBindingListBindingSource.DataSource = ThirdHandCommandBindingList.Revert();
            }
        }

        private void ImportButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                thirdHandCommandBindingListBindingSource.Clear();
                thirdHandCommandBindingListBindingSource.DataSource = ThirdHandCommandBindingList.LoadFromFile(openFileDialog1.FileName);
            }
        }
    }
}
